<?php
//
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
if($_RQ['op']=='main'){
	$id=$_RQ['id'];
	if(!$id){
		exi('请选择优惠券','error');
	}	
	$log=pdo_get('jy_weishop_coupon_getlog',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	$coupon=pdo_get('jy_weishop_coupon',array('uniacid'=>$_SESSION['uniacid'],'id'=>$log['couponid']));
		$log['past']=0;
		if($coupon['timelimittype']==1){
			$time=$log['createtime']+$coupon['timedays']*24*3600;
			if(time()>$time){
				$log['past']=1;
			}else{
				$start=$log['createtime'];
				$start=date('Y/m/d H:m:s',$start);
				$time=date('Y/m/d H:m:s',$time);
				$start=$log['createtime'];
				$log['time']=$start.'至'.$time;
			}
		}
		if($coupon['timelimittype']==2){
			$coupon['limittimes']=json_decode($coupon['limittimes'],true);
			
			if(time()>$coupon['limittimes']['tend']){
				$log['past']=1;
			}else{
				$coupon['limittimes']['tstart']=date('Y/m/d H:m:s',$coupon['limittimes']['tstart']);
				$coupon['limittimes']['tend']=date('Y/m/d H:m:s',$coupon['limittimes']['tend']);
				$log['time']=$coupon['limittimes']['tstart'].'至'.$coupon['limittimes']['tend'];
			}
		}
		$title2 = '';
		$title3 = '';
		if ($coupon['coupontype'] == '0') 
		{
			if (0 < $coupon['enough']) 
			{
				$title2 = '满' . (double) $coupon['enough'] . '元';
			}
			else 
			{
				$title2 = '购物任意金额';
			}
		}
		else if ($coupon['coupontype'] == '2') 
		{
			if (0 < $coupon['enough']) 
			{
				$title2 = '充值满' . (double) $coupon['enough'] . '元';
			}
			else 
			{
				$title2 = '充值任意金额';
			}
		}
		else if ($coupon['coupontype'] == '1') 
		{
			if (0 < $coupon['enough']) 
			{
				$title2 = '满' . (double) $coupon['enough'] . '元';
			}
			else 
			{
				$title2 = '购物任意金额';
			}
		}
		if ($coupon['backtype'] == 0) 
		{
			if ($coupon['enough'] == '0') 
			{
				$coupon['color'] = 'orange ';
			}
			else 
			{
				$coupon['color'] = 'blue';
			}
			$title3 = '减' . (double) $coupon['deduct'] . '元';
		}
		if ($coupon['backtype'] == 1) 
		{
			$coupon['color'] = 'red ';
			$title3 = '打' . (double) $coupon['discount'] . '折 ';
		}
		if ($coupon['backtype'] == 2) 
		{
			if (($coupon['coupontype'] == '0') || ($coupon['coupontype'] == '2')) 
			{
				$coupon['color'] = 'red ';
			}
			else 
			{
				$coupon['color'] = 'pink ';
			}
			if (!(empty($coupon['backmoney'])) && (0 < $coupon['backmoney'])) 
			{
				$title3 = $title3 . '送' . $coupon['backmoney'] . '元余额 ';
			}
			if (!(empty($coupon['backcredit'])) && (0 < $coupon['backcredit'])) 
			{
				$title3 = $title3 . '送' . $coupon['backcredit'] . '积分 ';
			}
			if (!(empty($coupon['backredpack'])) && (0 < $coupon['backredpack'])) 
			{
				$title3 = $title3 . '送' . $coupon['backredpack'] . '元红包 ';
			}
		}
		if ($log['past'] || !(empty($log['isused']))) 
		{
			$coupon['color'] = 'disa';
		}
		$coupon['title2'] = $title2;
		$coupon['title3'] = $title3;
	if($coupon['merchid']){
		$return['merchneme']=pdo_getcolumn('jy_weishop_merch',array('id'=>$coupon['merch_id']),'title');
	}
	$coupon['limitgoodids']= explode(';', $coupon['limitgoodids']);
	$coupon['limitgoodids']= substr(implode(',', $coupon['limitgoodids']),1,strlen(implode(',', $coupon['limitgoodids']))-2);
	$coupon['limitgoodcateids']= explode(';', $coupon['limitgoodcateids']);
	$coupon['limitgoodcateids']= substr(implode(',', $coupon['limitgoodcateids']),1,strlen(implode(',', $coupon['limitgoodcateids']))-2);
	if ($coupon['limitgoodtype'] != 0) {
			if (!empty($coupon['limitgoodids'])) {
				$where = 'and id in(' . $coupon['limitgoodids'] . ')';
			}

			$goods = pdo_fetchall('select `title` from ' . tablename('jy_weishop_goods') . ' where uniacid=:uniacid ' . $where, array(':uniacid' => $_SESSION['uniacid']), 'id');
		}

		if ($coupon['limitgoodcatetype'] != 0) {
			if (!empty($coupon['limitgoodcateids'])) {
				$where = 'and id in(' . $coupon['limitgoodcateids'] . ')';
			}

			$group = pdo_fetchall('select `title`  from ' . tablename('jy_weishop_goods_group') . ' where uniacid=:uniacid   ' . $where, array(':uniacid' => $_SESSION['uniacid']), 'id');
		}
	$return['setting']=plugin('jy_weishop')->plugin_setting('coupon');
	$return['goods']=$goods;
	$return['group']=$group;
	$return['log']=$log;
	$return['coupon']=$coupon;
	exi($return);
}

?>